package com.yanqu.road.dao.impl.yrank;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.yrank.UnionYRank;
import com.yanqu.road.entity.rank.yrank.UserYRank;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserYRankDaoImpl extends TempDataObjectDao<UserYRank> {

    public static String getUserYRankSql = "select * from `t_c_yrank_user` where `activityId`=?";

    public Map<Integer, Map<String, Map<Long, UserYRank>>> getUserYRank(int activityId) {

        Map<Integer, Map<String, Map<Long, UserYRank>>> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUserYRankSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserYRank parse = parse(rs);
                map.putIfAbsent(parse.getGroupId(), new ConcurrentHashMap<>());
                map.get(parse.getGroupId()).putIfAbsent(parse.getIdKey(), new ConcurrentHashMap<>());
                map.get(parse.getGroupId()).get(parse.getIdKey()).put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserYRankSql = "update `t_c_yrank_user` set userBaseInfo=?,value=?,param=?,updateTime=? where activityId=? and groupId=? and idKey=? and userId=?";

    @Override
    protected boolean updateData(UserYRank userYRank) {
        userYRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userYRank.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, userYRank.getValue().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYRank.getParam()));
        para.put(i++, new DbParameter(Types.BIGINT, userYRank.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userYRank.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userYRank.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYRank.getIdKey()));
        para.put(i++, new DbParameter(Types.BIGINT, userYRank.getUserId()));
        if (getDb().executeNoneQuery(updateUserYRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserYRankSql = "insert into `t_c_yrank_user`(activityId,groupId,idKey,userId,userBaseInfo,value,param,updateTime) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserYRank userYRank) {
        userYRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userYRank.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userYRank.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYRank.getIdKey()));
        para.put(i++, new DbParameter(Types.BIGINT, userYRank.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userYRank.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, userYRank.getValue().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYRank.getParam()));
        para.put(i++, new DbParameter(Types.BIGINT, userYRank.getUpdateTime()));
        if (getDb().executeNoneQuery(insertUserYRankSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserYRank parseData(ResultSet rs) throws SQLException {
        UserYRank userYRank = new UserYRank();
        userYRank.setActivityId(rs.getInt("activityId"));
        userYRank.setGroupId(rs.getInt("groupId"));
        userYRank.setIdKey(rs.getString("idKey"));
        userYRank.setUserId(rs.getLong("userId"));
        userYRank.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        userYRank.setValue(new BigInteger(rs.getString("value")));
        userYRank.setParam(rs.getString("param"));
        userYRank.setUpdateTime(rs.getLong("updateTime"));
        return userYRank;
    }

}
